package org.osflash.signals.natives;

import asunit.framework.IAsync;
import flash.display.Sprite;
import flash.events.MouseEvent;

class NativeMappedSignalDefaultsTest {

	@:meta(Inject())
	public var async : IAsync;
	var signalDefault : NativeMappedSignal;
	var signalDefaultWithMappingObject : NativeMappedSignal;
	var signalDefaultWithMappingFunction : NativeMappedSignal;
	var signalWithValueClassesWithoutMappingFunction : NativeMappedSignal;
	var sprite : Sprite;
	var EventType : String;
	var MappedObject : String;
	@:meta(Before())
	public function setUp() : Void {
		sprite = new Sprite();
		signalDefault = new NativeMappedSignal(sprite, EventType);
		signalDefaultWithMappingObject = new NativeMappedSignal(sprite, EventType).mapTo(MappedObject);
		signalDefaultWithMappingFunction = new NativeMappedSignal(sprite, EventType).mapTo(function() : String {
			return MappedObject;
		}
);
		signalWithValueClassesWithoutMappingFunction = new NativeMappedSignal(sprite, EventType, MouseEvent, String);
	}

	@:meta(After())
	public function tearDown() : Void {
		signalDefault.removeAll();
		signalDefault = null;
		signalDefaultWithMappingObject.removeAll();
		signalDefaultWithMappingObject = null;
		signalDefaultWithMappingFunction.removeAll();
		signalDefaultWithMappingFunction = null;
		signalWithValueClassesWithoutMappingFunction.removeAll();
		signalWithValueClassesWithoutMappingFunction = null;
	}

	//////
	@:meta(Test())
	public function signal_default_add_then_emptyCallback_should_be_called() : Void {
		signalDefault.add(async.add(emptyCallback, 10));
		sprite.dispatchEvent(new MouseEvent(EventType));
	}

	function emptyCallback(e : Dynamic = null) : Void {
	}

	@:meta(Test())
	public function signal_default_with_mapped_object_add_then_emptyCallback_should_be_called() : Void {
		signalDefaultWithMappingObject.add(async.add(emptyCallback, 10));
		sprite.dispatchEvent(new MouseEvent(EventType));
	}

	@:meta(Test())
	public function signal_default_with_mapped_function_add_then_emptyCallback_should_be_called() : Void {
		signalDefaultWithMappingFunction.add(async.add(emptyCallback, 10));
		sprite.dispatchEvent(new MouseEvent(EventType));
	}

	@:meta(Test(expects="ArgumentError"))
	public function signal_with_value_classes_without_mapping_function() : Void {
		signalWithValueClassesWithoutMappingFunction.dispatch(new MouseEvent(EventType));
	}

}

